from rest_framework import serializers

from app.goods.models import Goods, GoodsCategory, GoodsImage

'''
    # 使用serializer序列化
    class GoodListSerializer(serializers.Serializer):
        # 选择序列化的字段
        name = serializers.CharField(label='商品名', max_length=20)
        category = serializers.CharField(label='分类', max_length=10)

'''


class CategorySerializer3(serializers.ModelSerializer):
    class Meta:
        model = GoodsCategory
        fields = '__all__'


class CategorySerializer2(serializers.ModelSerializer):
    sub_cat = CategorySerializer3(many=True)

    class Meta:
        model = GoodsCategory
        fields = '__all__'


class CategorySerializer(serializers.ModelSerializer):
    sub_cat = CategorySerializer2(many=True)

    class Meta:
        model = GoodsCategory
        fields = '__all__'


class GoodsImageSerializer(serializers.ModelSerializer):
    class Meta:
        model = GoodsImage
        fields = ['image']


class GoodListSerializer(serializers.ModelSerializer):
    # 其中外键类别只显示类别的id，可以通过category的序列化进行嵌套
    category = CategorySerializer(help_text='分类')
    images = GoodsImageSerializer(many=True)

    class Meta:
        model = Goods
        exclude = ['market_price', 'goods_desc']
